package br.com.rotaestatistica.utils;

import java.sql.*;

public class FabricaDeConexao {
	
	private static Connection con;
	
	private static void CriaConexao() {

		String driver = "org.postgresql.Driver";
		String user = "postgres";

		/*
		 * nesse campo, o valor precisa ser mudado de acordo com o password que
		 * cada um definiu na hora de instalar o Postgre. Deixe em branco caso
		 * nao tenha definido password.
		 */
		String password = "";

		String url = "jdbc:postgresql://localhost:5432/postgres";

		try {
			Class.forName(driver);
			Connection con = null;

			con = (Connection) DriverManager.getConnection(url, user, password);
			FabricaDeConexao.con = con;
			
		} catch (ClassNotFoundException ex) {
			System.out.println("ClassNotFound");
			System.err.println(ex.getMessage());
		} catch (SQLException e) {
			System.out.println("SQLException");
			System.err.println(e.getMessage());
		}
	}
	
	public static Connection getConexao(){
		CriaConexao();
		return con;
	}
	
	public static void fechaConexao(){
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
